package integercoding;

import org.apache.commons.lang3.StringUtils;

public class GolombCoding {

	public static String simpleGolomb(int number, int m) {
		int q = (int) Math.floor((number - 1) / m);
		int r = number - 1 - q * m;
		int tam = (int) Math.ceil(Math.log(m) / Math.log(2));
		String result = "1";
		result = StringUtils.leftPad(result, q + 1, "0");
		String binr = Integer.toBinaryString(r);
		binr = StringUtils.leftPad(binr, tam, "0");
		return result + binr;
	}

	public static String actualGolomb(int number, int m) {
		int q = (int) Math.floor((number - 1) / m);
		int c = (int) Math.floor(Math.log(m) / Math.log(2));
		int r = number - 1 - q * m;
		String result = "1";
		result = StringUtils.leftPad(result, q + 1, "0");
		if (r < c) {
			String binr = Integer.toBinaryString(r);
			binr = StringUtils.leftPad(binr, c, "0");
			return result + binr;

		} else {
			String binr = Integer.toBinaryString(r + c);
			binr = StringUtils.leftPad(binr, c + 1, "0");
			return result + binr;

		}
	}
}
